/*
 * Copyright (c) 2008 Golden T Studios.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package mrusanov.sounds;

// JFC
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;

public class JavaLayerMp3Renderer extends BaseAudioRenderer {

	private Player player;

	@Override
	protected void playSound(final URL audiofile) {
				try {
					player = new Player(new BufferedInputStream(audiofile.openStream()), FactoryRegistry
							.systemRegistry().createAudioDevice());

					player.play();
				} catch (IOException e) {
					setStatus(Status.ERROR);
					System.err.println("Can not load audiofile (" + audiofile + ": " + e);
				} catch (JavaLayerException e) {
					setStatus(Status.ERROR);
					System.err.println("Problem playing audio: " + e);
				}
	}

	@Override
	public Status getStatus() {
		if (player != null) {
			// return EOS if the sound has been completed played
			return (player.isComplete()) ? Status.END_OF_SOUND : super.getStatus();
		}

		return super.getStatus();
	}
}
